package com.magic.smartcity.presenters;

import android.util.Log;

import com.magic.smartcity.beans.ServiceListBean;
import com.magic.smartcity.interfaces.ISystemServicesCallBack;
import com.magic.smartcity.interfaces.ISystemServicesModel;
import com.magic.smartcity.interfaces.ISystemServicesPresenter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * @description:
 * @create: Create by Magic on 2022/2/9.
 */
public class SystemServicePresenter implements ISystemServicesPresenter<ISystemServicesCallBack> {
    private static final String TAG = SystemServicePresenter.class.getSimpleName();

    private List<ISystemServicesCallBack> mISystemServicesCallBacks = new ArrayList<>();

    @Inject
    ISystemServicesModel mModel;

    @Inject
    SystemServicePresenter() {
    }
    @Override
    public void getAllService() {
        Log.d(TAG, "请求了全部服务");
        mModel.getAllService();
    }


    @Override
    public void registerCallBack(ISystemServicesCallBack callback) {
        if (!mISystemServicesCallBacks.contains(callback)) {
            Log.d(TAG,":::::注册了回调和事件总线");
            mISystemServicesCallBacks.add(callback);
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void unRegisterCallBack(ISystemServicesCallBack callback) {
        Log.d(TAG,":::::取消注册了回调和事件总线");
        EventBus.getDefault().unregister(this);
        mISystemServicesCallBacks.remove(callback);
    }


    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void eventBusListener(EventBusMessage message) {
        Log.d(TAG, "进入了eventBusListener::::" + message.allServices.size());
        for (ISystemServicesCallBack callBack : mISystemServicesCallBacks) {
            if (message.gallServices() != null) {//系统所有服务应用
                callBack.AllServiceLoaded(message.allServices);
            }


        }
    }

    public static class EventBusMessage {
        private List<ServiceListBean.RowsBean> allServices;

        public List<ServiceListBean.RowsBean> gallServices() {
            return allServices;
        }

        public void sellServices(List<ServiceListBean.RowsBean> allService) {
            this.allServices = allService;
        }
    }
}
